from datetime import datetime
from typing import Any

from mcp.server import FastMCP
from verifier import IntrospectionTokenVerifier
from mcp.server.auth.settings import AuthSettings
from pydantic import AnyHttpUrl

server_host = 'localhost'
server_port = 8080

issuer_host = 'localhost'
issuer_port = 9090

app=FastMCP(
    name="OAuth2 Demo",
    instructions="Resource Server that validates tokens via Authorization Server introspection",
    host=server_host,
    port=server_port,
    debug=True,
    token_verifier=IntrospectionTokenVerifier(
        introspect_endpoint=f"http://{issuer_host}:{issuer_port}/introspect"
    ),
    auth=AuthSettings(
        issuer_url=AnyHttpUrl(f"http://{issuer_host}:{issuer_port}"), # 授权服务器的url
        required_scopes=["user"],
        resource_server_url=AnyHttpUrl(f"http://{server_host}:{server_port}") # 资源服务器url
    )
)


@app.tool()
async def get_time() -> dict[str, Any]:
    """
    Get the current server time.

    This tool demonstrates that system information can be protected
    by OAuth authentication. User must be authenticated to access it.
    """

    now = datetime.now()

    return {
        "current_time": now.isoformat(),
        "timezone": "UTC",  # Simplified for demo
        "timestamp": now.timestamp(),
        "formatted": now.strftime("%Y-%m-%d %H:%M:%S"),
    }

if __name__ == '__main__':
    app.run(transport="streamable-http")